
var pro_load_page = 2,pro_load_flag = true;
var hist = localStorage.getItem('historycode');
var classify={
	cart : $('.list-btn'),
	navList: $(".index-nav li"),
	cartTop:null,//购物车图标距离顶部距离
	docTop:$(document).scrollTop(),
	listContainer : $('.product-list ul'),
	skuId:null,
	warehouseType:null,
	supplierId:null,
	init:function(){
		
		pro_load_flag =true;
		pro_load_page=2;
		var liw=$('.index-nav li').size()+40;//滚动宽度设置
		var ln=$('.index-nav li').length;
		$('.index-nav').attr('width',liw*ln);	
		var data=window.sessionStorage.getItem('nationClassify');
		var catid=JSON.parse(data).id;
		$('#aa').val(catid);
			app.ajax(
			'/Goods/Index/guojia_goods_list',
			{
				id : catid
			},
			function(res){
				if(res.code == 200){
					app.tpl($('.htb-list'),'tpl-htb-list',res);
					echo.init();
				}else{
					layer.msg(res.msg);
				}
			}
			);

	},
	addCart:function(t,pageX,pageY,pL){
		var addcar=$(t),
		cartPx=pageX,
		cartPy=pageY,
		cartTop=$(t).offset().top;
		this.skuId = $(t).attr("data-skuid");
		console.log(cartPy,cartPx);
		this.warehouseType = $(t).attr("data-warehousetype"); //仓库id
		this.supplierId = $(t).attr("data-supplierid");//供应商id
		console.log(this.supplierId);
		this.cartTop = $(t).offset().top;
		docTop=$(document).scrollTop();
		var data = {
			skuid:this.skuId,
			buyPoductNum :1,
			warehouseType : this.warehouseType,
			supplierId : this.supplierId
		}
		app.ajax(
			'/Goods/Cart/add_cart',
			data,function(res){
				console.log(JSON.stringify(res));
				if(res.code == 200){
					$('footer .index-cart i').text(res.num);
					var img = addcar.parent().parent().siblings('.img-p').find('img').attr('src');
					// 抛物线元素的的位置标记
					var flyer = $('<img class="u-flyer" src="'+img+'" width="60px" height="60px" style="z-index:350">');
					flyer.fly({
						start: {
							left: pL+10,
							top: cartTop-docTop+10
						},
						end: {
							left: cartPx,
							top: cartPy,
							width: 0,
							height: 0
						},
						onEnd: function(){
							$("#msg").show().animate({width: '250px'}, 200).fadeOut(1000);
							addcar.css("cursor","default").removeClass('orange').unbind('click');
							this.destory();
						}
					});
				}else if(res.code == 300){
					alert(res.msg);
				}
			}
		);
		
//		
//		var mess={
//			
//		}
		//app.store('cart_mess',)
	},
	navToggle:function(t,url,data){
		var catId = $(t).attr('data-id');
		this.navList.removeClass('font-active');
		$(t).siblings('li').addClass('font-normal');
		$(t).siblings('li').find('i').hide();
		$(t).removeClass('font-normal');
		$(t).find('i').show();
		$(t).addClass('font-active');
		$('#aa').val(catId);
		pro_load_flag = true;
		pro_load_page = 2;
		app.ajax(
			url,
			data,
			function(res){
				if(res.code == 200){
					app.tpl($('.product-list'),'tpl-product-list',res);
					echo.init();
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			}
		);
	},
	loadMore: function(url,catId){
		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.product-list').append(listLoadingEmpty);
   		console.log('catid:'+catId);
		pro_load_flag = false;
		app.ajax(
			url,
			{
				id : catId,
				page : pro_load_page,
				limit :20
			},
			function(res){
				$('.product-list .no-more').remove();
				if(res.code == 200){
					console.log('loadmORE');
					var list=res.list;
					if(res.list){
						for(var i=0,html='';i< list.length;i++){
							var str='';
							if(list[i].stock<1){
								str="<div class='sale-out'><div class='text'>已售馨</div></div>";
							}
							html+="<li class='fl w50' data-id='"+list[i].id+"' data-pid='"+list[i].pid+"'><div class='content bGWhite oF pL7 pR7'>"
									+"<div class='img-p pT10 oF pSR'><img src='../img/blank.gif' data-echo='"+list[i].pic+"' class='w'/>"+str+"</div>"
									+"<div class='list-detail pL5 '><div class='list-title w'><h2 class='fS14 lH18 limit-text mT14'>"+list[i].pname+"</h2></div>"
									+"<div class='detail w'><p class='fl mT20 mB15'><span class='cRed fS15'>&yen;"+list[i].price+"</span><s class='pL5 cGrey fS12'>&yen;"+list[i].market_price+"</s></p>"
									+"<div class='list-btn ddn-toCart mT10 mR3 tC bR50 bGBlack fr' data-skuId='"+list[i].id+"' data-warehousetype='"+list[i].warehouseType+"'  data-supplierId='"+list[i].ms_stock_supplierid+"'>"
									+"<img src='../img/list/cart1.png' class='w70 mT7'/></div></div><div></div></li>";
						}
						$('.product-list ul').append(html);
						echo.init();
						pro_load_page++;
						pro_load_flag = true;
					//page++;
					}else{						
						var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;">没有更多数据了~<div>';
						$('.product-list').append(listLoadingEmpty);
					}
				}else{
					layer.msg('系统繁忙，请稍候再试');
				}
			}
		);	
	}
	
}

classify.init();
footer.init();
window.onload = function(){

	 	$('.index-nav li:nth-child(1)').find('h2').addClass('font-active');
		$('.index-nav li:nth-child(1) i').show();
	 	app.toHistory(window.location.href);
	 	var cartPx=$('footer .index-cart').offset().left+$('footer .index-cart').width()/2,
		cartPy=$('footer .index-cart').offset().top+10;
		var catid=$(this).attr('data-catid');
		$('body').on('click','.list-btn',function(event){
			if($(this).attr('data-stock') <1){
				return;
			}
			var pageX=event.pageX;
			classify.addCart(this,cartPx,cartPy,pageX);
		});
		
			
		$(window).scroll(function(){
				echo.init();
				var data = window.sessionStorage.getItem('nationClassify'),
				dataUrl = '',
				catid=JSON.parse(data).id;
				// 当滚动到距底部以上200像素
				var offset = $(".product-list").offset();
				var documentHeight = offset.top + $(".product-list").height();
				if (documentHeight - $(this).scrollTop() - $(this).height()<700){
						dataUrl = '/Goods/Index/guojia_goods_list';
					pro_load_flag && classify.loadMore(dataUrl,catid);
				}
		});
		
		$('body').on('click','.product-list li .img-p',function(){
			var cat_id = $(this).attr('data-id');
			console.log('data-id',cat_id);
      		app.open('productDetail.html','productDetail',{id:cat_id},window.location.href,document.body.scrollTop);
		});
};




 
